package com.fgbf.todo.Activities;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.fgbf.todo.R;
import com.fgbf.todo.MainClasses.Category;
import com.fgbf.todo.MainClasses.Task;
import com.fgbf.todo.Managers.TaskManager;

public class ViewTaskActivity extends Activity {
	
	///////////////////////////////////////////////////////////////////////////////////
	// INSTANCE DATA
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * EditTexts
	 */
	private EditText nameEtxt, noteEtxt, streetEtxt, cityEtxt, stateEtxt, zipEtxt;
	
	/**
	 * TextViews
	 */
	private TextView title;
	
	/**
	 * Buttons
	 */
	private Button saveTskBttn, cancelTskBttn, mapBttn;
	
	/**
	 * Rating Bar
	 */
	private RatingBar priorityBar;
	
	/**
	 * The task manager
	 */
	private TaskManager tskMgr;
	
	/**
	 * The Category Spinner
	 */
	private Spinner catSpinner;
	
	/**
	 * The DatePicker for the due date
	 */
	private DatePicker dueDatePicker;
	
	/**
	 * The date for the DatePicker in milliseconds
	 */
	private long dueDate;
	
	
	///////////////////////////////////////////////////////////////////////////////////
	// METHODS
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * On Create Method, loads GUI
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newtask_screen);

		
		tskMgr = new TaskManager(ViewTaskActivity.this);
		
		priorityBar = (RatingBar) findViewById(R.id.ratingBar1);
		
		//title = (TextView) findViewById(R.id.textView1);
		
		nameEtxt = (EditText) findViewById(R.id.etxt_newtaskname);
		
		noteEtxt = (EditText) findViewById(R.id.etxt_newtasknote);
		
		streetEtxt = (EditText) findViewById(R.id.etxt_street);
		
		//cityEtxt = (EditText) findViewById(R.id.etxt_city);
		
		//stateEtxt = (EditText) findViewById(R.id.etxt_state);
		
		//zipEtxt = (EditText) findViewById(R.id.etxt_zip);
		
		saveTskBttn = (Button) findViewById(R.id.bttn_add_task);
		saveTskBttn.setOnClickListener(saveTaskListener);
		
		cancelTskBttn = (Button) findViewById(R.id.bttn_cancel_task);
		cancelTskBttn.setOnClickListener(cancelTaskListener);
		
		mapBttn = (Button) findViewById(R.id.bttn_view_map);
		mapBttn.setOnClickListener(mapBttnListener);
		
		catSpinner = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.category_array, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    catSpinner.setAdapter(adapter);
	    
	    dueDatePicker = (DatePicker) findViewById(R.id.datePicker1);
	    Calendar c = Calendar.getInstance();
	    dueDatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new dueDateListener());
		dueDate = c.getTimeInMillis();
		
		
		
		if(!tskMgr.getIsTaskNew(ViewTaskActivity.this)) {
			Task tempTask = tskMgr.getTaskFromDB(tskMgr.getTaskToBeViewed(ViewTaskActivity.this));
			
			nameEtxt.setText(tempTask.getName());
			
			noteEtxt.setText(tempTask.getNote());
			
			//title.setText("EDIT TASK");
			
			priorityBar.setRating(tempTask.getPriority());
			
			if (tempTask.getCategory().equals("PERSONAL"))
				catSpinner.setSelection(0);
			else if (tempTask.getCategory().equals("WORK"))
				catSpinner.setSelection(1);
			else if (tempTask.getCategory().equals("SCHOOL"))
				catSpinner.setSelection(2);
			
			Calendar tempCal = Calendar.getInstance();
			tempCal.setTimeInMillis(tempTask.getDueDate());
			dueDatePicker.updateDate(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH));
			
			streetEtxt.setText(tempTask.getAddrStreet());
		
			/*
			cityEtxt.setText(tempTask.getAddrCity());
			
			stateEtxt.setText(tempTask.getAddrState());
			
			zipEtxt.setText(tempTask.getAddrZip());
			*/
			
		}
		
		
		
	}
	//------------------------------------------------------------------------------------
	
	/**
	 * Switches to the TaskActivity when the back button is pressed
	 */
	public void onBackPressed() {
		startActivity(new Intent(this.getApplicationContext(), TaskActivity.class));
		ViewTaskActivity.this.finish();
		
	}
	//------------------------------------------------------------------------------------

	
	///////////////////////////////////////////////////////////////////////////////////
	// LISTENERS
	///////////////////////////////////////////////////////////////////////////////////

	/**
	 * Listener, responds to the Save Task Button
	 */
	private OnClickListener saveTaskListener = new View.OnClickListener() {
		public void onClick(View v) {
			if(tskMgr.getIsTaskNew(ViewTaskActivity.this)) {
				tskMgr.addTaskToDB(new Task(
						nameEtxt.getText().toString(), 
						noteEtxt.getText().toString(), 
						new Category(catSpinner.getSelectedItem().toString()), 
						(int)priorityBar.getRating(), 
						dueDate,
						streetEtxt.getText().toString(),
						"",
						"",
						""
						/*cityEtxt.getText().toString(),
						stateEtxt.getText().toString(),
						zipEtxt.getText().toString()*/));
				
				startActivity(new Intent(v.getContext(), TaskActivity.class));
				ViewTaskActivity.this.finish();
			}
		}
	};
	//------------------------------------------------------------------------------------
	
	/**
	 * Listener, repsonds to the Cancel Task Button
	 */
	private OnClickListener cancelTaskListener = new View.OnClickListener() {
		public void onClick(View v) {
			startActivity(new Intent(v.getContext(), TaskActivity.class));
			ViewTaskActivity.this.finish();
		}
	};
	//------------------------------------------------------------------------------------

	
	/**
	 * Listener, responds to the Due Date Picker
	 */
	private class dueDateListener implements OnDateChangedListener {

		public void onDateChanged(DatePicker arg0, int year, int month, int day) {
			Calendar c = Calendar.getInstance();
			c.set(year, month, day);
			dueDate = c.getTimeInMillis();
		}
	}
	//------------------------------------------------------------------------------------

	
	/**
	 * Listener, responds to view map button
	 */
	private OnClickListener mapBttnListener = new View.OnClickListener() {
		public void onClick(View v) {
			Intent i = new Intent(ViewTaskActivity.this, LocationActivity.class);
			i.putExtra("Location", streetEtxt.getText().toString() /*+ ", "
					+ cityEtxt.getText().toString() + ", "
					+ stateEtxt.getText().toString() + ", "
					+ zipEtxt.getText().toString()*/);
			startActivity(i);
		}
	};
	//---------------------------------------------------------------------------------

}
